package zhouzhouyongche.dao;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import zhouzhouyongche.model.Day;
import zhouzhouyongche.model.Hour;

@Repository
public class HourDao extends HibernateDao<Hour>{

	public Hour getOrAdd(int hour) {
		Hour result = null;
		String hql = "from Hour as h where h.hour = ?";
		Query q = getCurrentSession().createQuery(hql);
		q.setInteger(0, hour);
		List dayList = q.list();
		if (dayList.size() == 0) {
			Hour hourObj = new Hour();
			hourObj.setHour(hour);
			this.save(hourObj);
			
			String _hql = "from Hour as h where h.hour = ?";
			Query _q = getCurrentSession().createQuery(_hql);
			_q.setInteger(0, hour);
			result = (Hour)q.list().get(0);
		} else {
			result = (Hour)dayList.get(0);
		}
		return result;
	}
}
